package com.example.viewdemo

import android.app.AlertDialog
import android.os.Bundle
import android.util.Log
import android.view.View
import android.widget.Toast
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import com.example.viewdemo.databinding.ActivityAlertDialogBinding

class AlertDialogActivity : AppCompatActivity(), View.OnClickListener {

    private lateinit var binding: ActivityAlertDialogBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()

        binding= ActivityAlertDialogBinding.inflate(layoutInflater)
        setContentView(binding.root)
        binding.button4.text="普通"
        binding.button5.text="列表"
        binding.button6.text="check 选中框"
        binding.button4.setOnClickListener(this )
        binding.button5.setOnClickListener(this)
        binding.button6.setOnClickListener(this)
        binding.button7.setOnClickListener(this)
        binding.button8.setOnClickListener(this)
    }

    fun showNormalDialog(view: View) {
        val context = this

        // 创建并显示 AlertDialog
        AlertDialog.Builder(context)
            .setTitle("标题") // 设置对话框标题
            .setMessage("这是一个普通的AlertDialog") // 设置对话框消息
            .setPositiveButton("确定") { dialog, which ->
                // 确定按钮的点击事件
            }
            .setNegativeButton("取消") { dialog, which ->
                // 取消按钮的点击事件
                dialog.dismiss() // 关闭对话框
            }
            .show() // 显示对话框
    }

    fun showLessonDialog(view: View) {
        val context = this

        val lesson = arrayOf("语文", "数学", "英语", "化学", "生物", "物理", "体育")
        val builder = AlertDialog.Builder(context)
            .setTitle("选择你喜欢的课程")
//            .setMessage("下拉框") // 设置对话框消息
            .setItems(lesson) { dialog, which ->
                Toast.makeText(context, "你选择了${lesson[which]}", Toast.LENGTH_SHORT).show()
            }
            .create()
              .show()
    }

    fun showFruitDialog(view: View) {
        val context = this
        val fruits = arrayOf("苹果", "雪梨", "香蕉", "葡萄", "西瓜")
        val builder = AlertDialog.Builder(context)
            .setTitle("选择你喜欢的水果，只能选一个哦~")
            .setSingleChoiceItems(fruits, 0) { dialog, which ->
                Toast.makeText(context, "你选择了${fruits[which]}", Toast.LENGTH_SHORT).show()
            }
            .create()
          .show()
    }

    override fun onClick(v: View?) {
        Log.d("AlertDialogActivity", "按键按下")
        v?.let {
            when(it.id) {
                R.id.button4 -> showNormalDialog(it)
                R.id.button5 -> {
                    // 显示另一个对话框
                    showLessonDialog(v)
                }
                R.id.button6 -> {
                    // 显示另一个对话框
                    showFruitDialog(v)
                }
                R.id.button7 -> {
                    // 显示另一个对话框
                }
                R.id.button8 -> {
                    // 显示另一个对话框
                    binding.button4.visibility = View.INVISIBLE
                }
            }
        }
    }
}